package com.dementors.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import com.dementors.R;
import com.dementors.model.FindItemBean;
import com.dementors.widget.NineImageView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;

/**
 * Author  : itxuye(itxuye@gmail.com)|(http://itxuye.com)
 * Date    : 2015-12-04
 * Time    : 14:20
 * FIXME
 */
public class FindAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

  private List<FindItemBean.TimelinesEntity> itemList;

  private Context mContext;

  public FindAdapter(Context context) {
    Log.i("itxuye", "context");
    this.mContext = context;
    itemList = new ArrayList<>();
  }

  public void updateWithClear(List<FindItemBean.TimelinesEntity> list) {
    itemList.clear();
    itemList.addAll(list);
    notifyDataSetChanged();
  }

  public void update(List<FindItemBean.TimelinesEntity> list) {
    Log.i("itxuye", "update");
    itemList.addAll(list);
    notifyDataSetChanged();
  }

  public void updateStart(List<FindItemBean.TimelinesEntity> list) {

    itemList.addAll(0, list);
    notifyDataSetChanged();
  }

  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Log.i("itxuye", "onCreateViewHolder");
    View view;
    if (viewType == 101) {
      view = LayoutInflater.from(parent.getContext())
          .inflate(R.layout.find_101_item, parent, false);
      return new FindItem101VH(view);
    } else if (viewType == 1) {
      view = LayoutInflater.from(parent.getContext())
          .inflate(R.layout.find_1_item, parent, false);
      return new FindItem1VH(view);
    }
    return null;
  }

  @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    Log.i("itxuye", "onBindViewHolder");
    if (holder instanceof FindItem101VH) {
      FindItemBean.TimelinesEntity timelinesEntity = itemList.get(position);

      Picasso.with(mContext)
          .load(timelinesEntity.getRecommendUser().getAvatar())
          .placeholder(R.drawable.img_pintuan_head)
          .into(((FindItem101VH) holder).avatar);

      ((FindItem101VH) holder).textView_nick.setText(
          timelinesEntity.getRecommendUser().getNick());

      ((FindItem101VH) holder).textView_introduce.setText(
          timelinesEntity.getRecommendUser().getIntroduce());
    } else if (holder instanceof FindItem1VH) {
      FindItemBean.TimelinesEntity timelinesEntity = itemList.get(position);

      Picasso.with(mContext)
          .load(timelinesEntity.getMoment().getUserInfo().getAvatar())
          .placeholder(R.drawable.img_pintuan_head)
          .into(((FindItem1VH) holder).avatar);

      ((FindItem1VH) holder).textView_gmt_create.setText(
          timelinesEntity.getMoment().getGmtCreate());

      ((FindItem1VH) holder).textView_nick.setText(
          timelinesEntity.getMoment().getUserInfo().getNick());

      ((FindItem1VH) holder).textItem_detail.setText(
          timelinesEntity.getMoment().getContent());

      //九个图片加载

      List<String> list = new ArrayList<>();
      List<String> list1 = new ArrayList<>();
      if ((timelinesEntity.getMoment().getImgs() != null)) {
        List<FindItemBean.TimelinesEntity.MomentEntity.ImgsEntity> imgs =
            timelinesEntity.getMoment().getImgs();

        for (FindItemBean.TimelinesEntity.MomentEntity.ImgsEntity imgsss : imgs) {

          String url = imgsss.getUrl();
          list.add(url);
        }
        ((FindItem1VH) holder).nineImageView.setImageList(list, R.drawable.default_80_80,
            R.drawable.default_80_80);
      } else if ((timelinesEntity.getMoment().getProduct().getImgs() != null)) {
        List<FindItemBean.TimelinesEntity.MomentEntity.ProductEntity.ImgsEntity> imgs1 =
            timelinesEntity.getMoment().getProduct().getImgs();
        for (FindItemBean.TimelinesEntity.MomentEntity.ProductEntity.ImgsEntity imgss : imgs1) {

          String url = imgss.getUrl();
          list1.add(url);
        }
        ((FindItem1VH) holder).nineImageView.setImageList(list1, R.drawable.default_80_80,
            R.drawable.default_80_80);
      }
    }
  }

  @Override public int getItemCount() {
    Log.i("itxuye", itemList.size() + "");
    return itemList.size();
  }

  @Override public int getItemViewType(int position) {
    Log.i("itxuye", itemList.get(position).getType() + "");
    return itemList.get(position).getType();
  }

  static class FindItem101VH extends RecyclerView.ViewHolder {

    @Bind(R.id.tuijian) ImageView tuijian;
    @Bind(R.id.avatar) ImageView avatar;
    @Bind(R.id.textView_101_1) TextView textView_nick;
    @Bind(R.id.textView_101_2) TextView textView_introduce;

    public FindItem101VH(View itemView) {
      super(itemView);
      Log.i("itxuye", "FindItem101VH");
      ButterKnife.bind(this, itemView);
    }
  }

  static class FindItem1VH extends RecyclerView.ViewHolder {

    @Bind(R.id.avatar) ImageView avatar;
    @Bind(R.id.textView_101_1) TextView textView_nick;
    @Bind(R.id.textView_101_2) TextView textView_gmt_create;
    @Bind(R.id.text_item) TextView textItem_detail;
    @Bind(R.id.nineImageView) NineImageView nineImageView;

    public FindItem1VH(View itemView) {
      super(itemView);
      Log.i("itxuye", "FindItem1VH");
      ButterKnife.bind(this, itemView);
    }
  }
}
